package com.cheers.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.StaleObjectStateException;
import org.springframework.util.StringUtils;

import com.cheers.common.Constants;
import com.cheers.common.ResultListModel;
import com.cheers.common.ResultModel;
import com.cheers.service.IntegralService;
import com.cheers.service.ProductService;
import com.cheers.util.VerifyUtil;

public class ProductAction extends BaseAction {

	private static final long serialVersionUID = -3843713977256310917L;

	@Resource
	private ProductService productService;
	@Resource
	private IntegralService integralService;
	
	private String userId;
	private String productId;
	private String pageIndex;
	private String number;
	
	
	/**
	 * 购买商品
	 * @return
	 * @throws Exception
	 */
	public String buy() throws Exception {
		//验证
		if (StringUtils.isEmpty(userId)) {
			result = new ResultModel(Constants.NO, Constants.USERID_DESC, "");
			return SUCCESS;
		}
		if (StringUtils.isEmpty(productId)) {
			result = new ResultModel(Constants.NO, Constants.PRODUCTID_DESC, "");
			return SUCCESS;
		}
		if (!VerifyUtil.numberTest(number)) {
			result = new ResultModel(Constants.NO, Constants.LUCK_NUM_DESC, "");
			return SUCCESS;
		}
		if (!isPost("buy")) {
			result = new ResultModel(Constants.NO, Constants.INVALID_DESC, "");
			return SUCCESS;
		}
		
		int num = Integer.parseInt(number);
		//用积分兑换商品
		String data = null;
		try {
			data = productService.buy(userId, productId, num);
		} catch (StaleObjectStateException e) {
			try {
				data = productService.buy(userId, productId, num);
			} catch (Exception e1) {
				result = new ResultModel(Constants.NO, Constants.NO_DESC, null);
				return SUCCESS;
			}
		}
		if (data == null) {
			result = new ResultModel(Constants.PRODUCT_CODE, Constants.PRODUCT_DESC, null);
			return SUCCESS;
		} else if(Constants.PRODUCT_NOT_CODE.equals(data)) {
			num = productService.lastChance(productId);
			result = new ResultModel(Constants.PRODUCT_NOT_CODE, Constants.PRODUCT_NOT_DESC+num, null);
			return SUCCESS;
		} else if (Constants.PRODUCT_FULL_CODE.equals(data)) {
			result = new ResultModel(Constants.PRODUCT_FULL_CODE, Constants.PRODUCT_FULL_DESC, null);
			return SUCCESS;
		} else if (Constants.USER_CODE.equals(data)) {
			result = new ResultModel(Constants.USER_CODE, Constants.USER_DESC, null);
			return SUCCESS;
		} else if (Constants.POINT_TOTAL_CODE.equals(data)) {
			result = new ResultModel(Constants.POINT_TOTAL_CODE, Constants.POINT_TOTAL_DESC, null);
			return SUCCESS;
		}
		//获取用户总积分
		Long total = integralService.getTotalPoint(userId);
		result = new ResultModel(Constants.OK, Constants.OK_DESC, String.valueOf(total));
		return SUCCESS;
	}
	
	/**
	 * 获取商品列表
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {
		//验证
		if (StringUtils.isEmpty(pageIndex)) {
			pageIndex = "0";
		}
		if (!VerifyUtil.numberTest(pageIndex)) {
			result = new ResultModel(Constants.NO, Constants.PAGE_NUM_DESC, "");
			return SUCCESS;
		}
		
		int page = Integer.parseInt(pageIndex);
		//获取商品列表
		List<Map<String, Object>> list = productService.list(page, Constants.PAGESIZE);
		if (list!=null && !list.isEmpty()) {
			result = new ResultListModel(list, pageIndex, Constants.PAGESIZE, list.size());
		} else {
			result = new ResultListModel(new Object[0], pageIndex, Constants.PAGESIZE, 0);
		}
		return SUCCESS;
	}
	
	/**
	 * 获取兑换记录列表
	 * @return
	 * @throws Exception
	 */
	public String exchangeList() throws Exception {
		//验证
		if (StringUtils.isEmpty(userId)) {
			result = new ResultModel(Constants.NO, Constants.USERID_DESC, "");
			return SUCCESS;
		}
		if (StringUtils.isEmpty(pageIndex)) {
			pageIndex = "0";
		}
		if (!VerifyUtil.numberTest(pageIndex)) {
			result = new ResultModel(Constants.NO, Constants.PAGE_NUM_DESC, "");
			return SUCCESS;
		}
		
		int page = Integer.parseInt(pageIndex);
		//获取兑换记录
		List<Map<String, Object>> list = productService.exchangeList(userId, page, Constants.PAGESIZE);
		if (list!=null && !list.isEmpty()) {
			result = new ResultListModel(list, pageIndex, Constants.PAGESIZE, list.size());
		} else {
			result = new ResultListModel(new Object[0], pageIndex, Constants.PAGESIZE, 0);
		}
		return SUCCESS;
	}

	
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getProductId() {
		return productId;
	}
	public void setProductId(String productId) {
		this.productId = productId;
	}
	public String getPageIndex() {
		return pageIndex;
	}
	public void setPageIndex(String pageIndex) {
		this.pageIndex = pageIndex;
	}
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	
}
